unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

const
  N = 10;
  M = 20;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure TBitBtn1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr : array [0..(N*N-1)] of TBitBtn;
  min: array [0..(M-1)] of integer;
  mi: array [0..(N*N-1)] of integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: integer;
begin
  for i:=0 to N*N-1 do
  begin
    mi[i]:=0;
  end;
  randomize;
  for i:=0 to M-1 do
  begin
    min[i]:=random(N*N-1);
    for j:=0 to i-1 do
    begin
      if i <> j then
      begin
        while min[i] = min[j] do
        begin
          min[i]:=random(N*N-1);
        end;
      end;
    end;
  end;
  for i:=0 to N*N-1 do
  begin
    arr[i]:=TBitBtn.Create(Form1);
    arr[i].Width:=20;
    arr[i].Height:=20;
    arr[i].Left:= 50 + (i mod N)*20;
    arr[i].Top:= 50 + (i div N)*20;
    arr[i].Parent:=Form1;
    arr[i].OnClick:=TBitBtn1Click;
  end;
end;

procedure TForm1.TBitBtn1Click(Sender: TObject);
var
  i, j, k: integer;
begin
  for i:=0 to N*N-1 do
  begin
    if (Sender = arr[i]) then
    begin
      for j:=0 to M-1 do
      begin
        if i = min[j] then
        begin
          arr[i].Caption:= '*';
          Application.MessageBox('You are dead!', 'Game over');
          for k:= 0 to N*N-1 do
          begin
            arr[k].Enabled:=False;
          end;
        end else begin
          if ((i+1) = min[j]) and (i mod N <> N-1) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i-1) = min[j]) and (i mod N <> 0) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i-N) = min[j]) and (i - N >= 0) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i+N) = min[j]) and (i + N <= N*N) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i-N-1) = min[j]) and (i - N > 0) and (i mod N <> 0) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i+N-1) = min[j]) and (i + N <= N*N) and (i mod N <> 0) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i+N+1) = min[j]) and (i + N < N*N) and (i mod N <> N-1) then
          begin
            mi[i]:=mi[i]+1;
          end;
          if ((i-N+1) = min[j]) and (i - N >= 0) and (i mod N <> N-1) then
          begin
            mi[i]:=mi[i]+1;
          end;
        end;
      end;
    end;
  end;
  for i:=0 to N*N-1 do
  begin
    if (arr[i].Caption = '') and (Sender = arr[i]) then
    begin
      arr[i].Caption:= intToStr(mi[i]);
    end;
  end;
  j:=0;
  for i:=0 to N*N-1 do
  begin
    if arr[i].Caption <> '' then
    begin
      j:=j+1;
    end;
  end;
  if j = N*N-M then
  begin
    Application.MessageBox('You won!', 'Congratulations!');
    for k:= 0 to N*N-1 do
    begin
      arr[k].Enabled:=False;
    end;
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var j, i: integer;
begin
  for j:=0 to N*N-1 do
  begin
    arr[j].Caption:= '';
    arr[j].Enabled:=True;
  end;
  for i:=0 to N*N-1 do
  begin
    mi[i]:=0;
  end;
  randomize;
  for i:=0 to M-1 do
  begin
    min[i]:=random(N*N-1);
    if i > 0 then
    begin
      for j:=0 to i-1 do
      begin
        if min[i] = min[j] then
        begin
          while min[i] = min[j] do
          begin
            min[i]:=random(N*N-1);
          end;
        end;
      end;
    end;
  end;
end;

end.
